package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCargoPublishPublisherBinding;
import com.sgb.kjwl.di.transport.DaggerTransportComponent;
import com.sgb.kjwl.di.transport.TransportModule;
import com.sgb.kjwl.model.entity.transport.CargoPublishEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.viewmodel.transport.CargoPublishPublisherViewModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 2020/12/29 17:50
 * @Description: 货源发布填写发布人信息页面
 */
public class CargoPublishPublisherActivity extends AppActivity {

    @Inject
    CargoPublishPublisherViewModel viewModel;
    ActivityCargoPublishPublisherBinding binding;
    //数据实体类
    private CargoPublishEntity mEntity;

    public static void start(Activity activity, CargoPublishEntity entity, int requestCode) {
        Intent intent = new Intent(activity, CargoPublishPublisherActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra(RouterKey.DATA, entity);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerTransportComponent.builder()
                .appComponent(appComponent)
                .transportModule(new TransportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_publish_publisher);
        binding.setLifecycleOwner(this);
        binding.includeTitle.titleView.setText("信息发布人");
        initEvent();
        initData();
    }

    private void initEvent() {
        binding.setOnClick(this);
        binding.includeTitle.ivBack.setOnClickListener(this);
    }

    private void initData() {
        mEntity = getIntent().getParcelableExtra(RouterKey.DATA);
        if (mEntity == null) {
            mEntity = new CargoPublishEntity();
        }
        if (TextUtils.isEmpty(mEntity.getCompanyNo())) {
            //设置用户信息
            mEntity.setCompanyNo(UserOperating.getInstance().getTeamOrCompanyNameNo());
            mEntity.setCompany(UserOperating.getInstance().getTeamOrCompanyName());
            mEntity.setContact(UserOperating.getInstance().getUserRealName());
            mEntity.setContactTel(UserOperating.getInstance().getPhone());
        }
        syncData();
    }

    private void syncData() {
        if (!TextUtils.isEmpty(mEntity.getCompany())) {
            binding.tvReleaseCargoCompany.setText(mEntity.getCompany());
        }
        if (!TextUtils.isEmpty(mEntity.getContact())) {
            binding.etReleaseCargoContact.setText(mEntity.getContact());
        }
        if (!TextUtils.isEmpty(mEntity.getContactTel())) {
            binding.etReleaseCargoContactTel.setText(mEntity.getContactTel());
        }
    }

    @Override
    public void onClick(View v) {
        if (v == binding.includeTitle.ivBack) {
            finish();
        } else if (v == binding.tvReleaseCargoSubmit) {
            clearAllFocus();
            checkData();
        }
    }

    /**
     * 检查数据并回传
     */
    private void checkData() {
        if (mEntity == null) return;
        String contact = binding.etReleaseCargoContact.getText().toString();
        String tel = binding.etReleaseCargoContactTel.getText().toString();
        if (TextUtils.isEmpty(contact)) {
            MToast.showToast(this, "请输入联系人");
            return;
        }
        if (TextUtils.isEmpty(tel)) {
            MToast.showToast(this, "请输入联系电话");
            return;
        }
        //设置联系人/联系人电话
        mEntity.setContact(contact);
        mEntity.setContactTel(tel);
        Intent intent = new Intent();
        intent.putExtra(RouterKey.DATA, mEntity);
        setResult(RESULT_OK, intent);
        finish();
    }
}
